﻿using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Map;
using Map_Creator.AnimatedTileEditor;
using Map_Creator.MapInfo;
using Map_Creator.RedoUndo;
using Map_Creator.RedoUndo.Actions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Map_Creator.AutoTileEditor
{
    public partial class AutoTiles5AutoDesigner : XNA.GraphicsDeviceControl
    {
        private Texture2D MappingTexture;

        TileStruct[] tiles = new TileStruct[6];
        private TileSet tileSet;
        private TileSetControl tileSetControl;
        private IRedoUndo actionHandler;

        public AutoTiles5AutoDesigner()
        {
            InitializeComponent();
        }

        public AutoTiles5AutoDesigner(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        public void Initialize(AutoTileData tileData, TileSet set, TileSetControl control, IRedoUndo redoUndo)
        {
            tileSet = set;
            tileSetControl = control;
            actionHandler = redoUndo;
            actionHandler.OnRedoUndo += actionHandler_OnRedoUndo;
            var content = new ContentManager(Services,"Content");
            MappingTexture = content.Load<Texture2D>("AutoTile5AutoMap");

            if (tileData != null && tileData.Type == AutoTileType.AutoTile5)
            {
                tiles = tileData.Tiles;
            }
        }

        void actionHandler_OnRedoUndo(IRedoUndo sender, IAction action)
        {
            Invalidate();
        }

        protected override void Draw(PaintEventArgs e)
        {
            GraphicsDevice.Clear(Color.FromNonPremultiplied(BackColor.R, BackColor.G, BackColor.B, BackColor.A));
            xnaGraphics.Begin();

            xnaGraphics.Draw(MappingTexture,Vector2.Zero,Color.White);

            var tileSize = tileSet.AssociatedMap.TileSize;
            var tilesSize = tileSet.TilesSize;

            for (int i = 0; i < tiles.Length; i++)
            {
                if (tiles[i] == null) continue;
                
                var dest = new Rectangle((i % 3) * 32, (i / 3) * 32, 32, 32);
                xnaGraphics.Draw(tileSet.TilesImage, dest,
                                 new Rectangle(
                                     (tiles[i].Value % tilesSize.X) * tileSize,
                                     (tiles[i].Value/ tilesSize.X) * tileSize,
                                     tileSize, tileSize),
                                 Color.White);
            }

            xnaGraphics.End();
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            switch(e.Button)
            {
                case MouseButtons.Left:
                    LeftClick(e);
                    break;
                case MouseButtons.Right:
                    RightClick(e);
                    break;
            }
        }

        private void RightClick(MouseEventArgs e)
        {
            var pos = MathEx.Clamp(e.X, 0, 98) / 32 + (MathEx.Clamp(e.Y, 0, 64) / 32) * 3;
            actionHandler.AddActionAndExecute(new ArrayReplaceAction(tiles, new[] { pos },null));

            Invalidate();
        }

        private void LeftClick(MouseEventArgs e)
        {
            var pos = MathEx.Clamp(e.X, 0, 98)/32 + (MathEx.Clamp(e.Y, 0, 64)/32)*3;
            actionHandler.AddActionAndExecute(
                new ArrayReplaceAction(tiles, new[] { pos }, tileSetControl.GetSelectedTile()));

            Invalidate();
        }

        public AutoTileData GetAutoTile()
        {
            if (tiles.Where((t, i) => i != 2).Any(t => t == null))
            {
                return null;
            }

            return new AutoTileData(AutoTileType.AutoTile5, tiles);
        }
    }
}